<template>
  <div class="app-container">
    <el-row class="library-row">
 
    </el-row>
    <el-table
      :data="tableData"
      stripe
       @sort-change="sortChange"
      style="width: 100%"
      :default-sort="{prop: 'clicks', order: 'descending'}"
    >
      <el-table-column prop="id" label="#"></el-table-column>
      <el-table-column prop="title" label="图书名" align="center"></el-table-column>
       <el-table-column prop="author" label="作者" width="250" align="center"></el-table-column>
        <el-table-column prop="publisher" label="出版社" width="350" align="center"></el-table-column>
         <el-table-column prop="category_name" label="分类" width="200" align="center"></el-table-column>
         <el-table-column prop="clicks" label="阅读量" width="350" align="center" sortable="custom"></el-table-column>
    </el-table>
    <el-row class="total">
      <el-pagination
        @current-change="currentPage"
        background
        :page-size="20"
        layout="prev, pager, next"
        :total="total"
      ></el-pagination>
    </el-row>
  </div>
</template>

<script>
import {
  getClicks
} from "@/api/statistical";

export default {
  data() {
    return {
      total: 1,
      form: {
        page: 1,
        page_size: 20,
        sort:'clicks',
        order:"desc"
      },
      listLoading: true,
      tableData: [],
    };
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      let params=this.form
      getClicks(params).then((response) => {
        console.log(response);
        this.total = response.total;
        this.tableData = response.data;

        this.listLoading = false;
      });
    },
    currentPage(val) {
      this.form.page = val;
       this.fetchData()
    },
        // 排序
    sortChange(val){
      let order=val.order
      if(order=='descending'){
        this.form.order='desc'
      }else if(order=='ascending'){
        this.form.order='asc'
      }
    this.fetchData()
    },
  
  },
};
</script>
<style>
.library-row {
  padding: 15px 0;
}

.add {
  display: flex;
  justify-content: flex-end;
}
.el-dialog__body {
  border: 1px solid #ddd;
}
.total {
  display: flex;
  justify-content: center;
  padding: 40px 0;
}
</style>